package com.example.waimaiandroid.address;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.waimai.HttpUtil.MyPost;
import com.example.waimai.context.UserInfoContext;
import com.example.waimai.pojo.User;
import com.example.waimai.pojo.UserAddress;
import com.example.waimai.util.MyThreadPool;
import com.example.waimaiandroid.R;
import com.example.waimaiandroid.addAddress.addAddress;
import com.example.waimaiandroid.billing.BillingActivity;


import java.util.List;

public class ReceiveAddrAdapter extends RecyclerView.Adapter<ReceiveAddrAdapter.ViewHolder> {


    public List<UserAddress> userAddressList;

    private AddressActivity activity;

    public ReceiveAddrAdapter(List<UserAddress> userAddressList,AddressActivity activity){
        this.userAddressList = userAddressList;
        this.activity = activity;
    }

    public void setSelectAddrListener(View view,ViewHolder vh){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回结果给 BillingActivity
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                int position = vh.getAdapterPosition();
                // 传入position
                bundle.putSerializable("userAddress",userAddressList.get(position));
                intent.putExtras(bundle);
                intent.setComponent(new ComponentName(activity, BillingActivity.class));
                activity.setResult(0,intent);
                activity.finish();

            }
        });

    }
    private boolean isSetSelectListner = false;
    public void setSetSelectListner(boolean flag){
        isSetSelectListner = flag;
        // 感觉需要将菜单选项给禁用
    }

    // 创建视图，将视图和 ViewHolder 绑定
    // 返回一个 ViewHolder
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.receive_addr_item,null);
        ViewHolder vh = new ViewHolder(view);
        if(isSetSelectListner){
            setSelectAddrListener(view,vh);
        }

        return vh;
    }

    // 设置数据
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            position = holder.getAdapterPosition();
            UserAddress userAddress = userAddressList.get(position);
            holder.receive_address.setText(userAddress.getAddress());
            holder.receive_bottome.setText(userAddress.getBottom());
            holder.receive_name.setText(userAddress.getReceiveName());
            holder.receive_tele.setText(userAddress.getTelephone());
        // 对 每一项的组件设置监听事件
        if(!isSetSelectListner){
            // 更新
            holder.receive_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position1 = holder.getAdapterPosition();
                    // 修改地址
                    Log.e("测试修改地址",userAddressList.get(position1).toString());
                    //携带参数跳转过去
                    Intent intent  = new Intent();
                    Bundle bundle = new Bundle();
                    intent.putExtra("fromActivity","updateAddress");
                    bundle.putSerializable("userAddress",userAddressList.get(position1));
                    intent.putExtra("position",position1);
                    intent.putExtras(bundle);
                    intent.setComponent(new ComponentName(AddressActivity.getActivity(), addAddress.class));
                    AddressActivity.getActivity().startActivity(intent);
                    AddressActivity.getActivity().finish();
                }
            });
            // 删除 position 应该弄成动态的。
            holder.receive_remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position2 = holder.getAdapterPosition();
                    // 删除地址
                    userAddress.setStatus(0);
                    MyThreadPool.doJob(new Runnable() {
                        @Override
                        public void run() {
                            Boolean ack = (Boolean) MyPost.doPost("updateUserAddress",userAddress);
                            Message msg = new Message();
                            if(ack){
                                msg.what = 1;
                                msg.obj = position2;
                            }else{
                                // 删除失败
                                msg.what = 2;
                                userAddress.setStatus(1);
                            }
                            activity.handler.sendMessage(msg);
                        }
                    });

                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return userAddressList.size();
    }



    class ViewHolder extends RecyclerView.ViewHolder {

        TextView receive_address;
        TextView receive_bottome;
        ImageView receive_update;
        TextView receive_name;
        TextView receive_tele;
        ImageView receive_remove;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            receive_address = itemView.findViewById(R.id.receive_address);
            receive_bottome = itemView.findViewById(R.id.receive_bottome);
            receive_update = itemView.findViewById(R.id.receive_update);
            receive_name = itemView.findViewById(R.id.receive_name);
            receive_tele = itemView.findViewById(R.id.receive_tele);
            receive_remove = itemView.findViewById(R.id.receive_remove);
        }
    }


}
